﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="newClient.aspx.cs" Inherits="OpenRAMS.Community.newClient" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html />

<script runat="server">
    void btnClient_Click(Object sender, EventArgs e)
    {
        //test for a parameter
        Boolean paramTest = false;
        String clientRequest = "";
        try
        {
            clientRequest = Request.QueryString["cID"].ToString();
        }
        catch { paramTest = true; }

        ClientRecord nClient = new ClientRecord();
        
        //if there was a parameter, load it in
        if (!paramTest)
        {
            String input = clientRequest.Replace("'", "");
            
            nClient.Load(input);
        }
        else
        {
            nClient.clientName = txtClientName.Text;    //this is only required for new records
        }
        
        //No verification required as most of this is done by front-end controls. We can safely store nulls for most values
        //(aside from client name. It is disabled by the load routine so can be safely ignored)
        nClient.clientOrg = txtClientOrg.Text;
        nClient.clientJobTitle = txtJTitle.Text;
        nClient.clientJobDept = txtDept.Text;
        nClient.clientColour = txtColChoice.Text;
        nClient.clientPhoneLandLine = txtLandLine.Text;
        nClient.clientPhoneMobile = txtMobile.Text;
        nClient.clientAddEmail = txtEmailAddress.Text;
        nClient.clientAddFirstLine = txtAd1.Text;
        nClient.clientAddSecondLine = txtAd2.Text;
        nClient.clientAddThirdLine = txtAd3.Text;
        nClient.clientAddPostCode = txtPostCode.Text;

        int test = nClient.Save();
        if (test < 0)
        {
            lblError.Visible = true;
            lblError.Text = "Error: Did you type in valid values?";
        }
        else
        {
            Response.Redirect("Community.aspx");
        }       
       
    }

    void btnCCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Community.aspx");
    }


</script>

<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Request Booking</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <style type="text/css">
        .style1
        {
            width: 220px;
        }
    </style>

    <%
        
        //Verify user is in Community role (if they aren't send them back to home)
        if (!User.IsInRole("community") && !User.IsInRole("admin")) { Response.Redirect("~/Staff/Staff.aspx"); }

        //This page supports parameters!
        //Grab work ticket ID from URL parameter (if it isn't present, set boolean)
        Boolean paramTest = false;
        String clientRequest = "";
        try
        {
            clientRequest = Request.QueryString["cID"].ToString();
        }
        catch { paramTest = true; }

        if (!paramTest)
        {
            //if all is good, test to see if there is a new record containing that 
            Clients clientList = new Clients();
            clientList.Populate("txtClientName", clientRequest);
            List<ClientRecord> clients = clientList.getClientList();

            if (clients.Count() == 1)
            {
                foreach (ClientRecord client in clients)
                {
                    txtClientName.Text = client.clientName;
                    txtClientName.Enabled = false;
                    
                    txtClientOrg.Text = client.clientOrg;
                    txtJTitle.Text = client.clientJobTitle;
                    txtDept.Text = client.clientJobDept;
                    txtLandLine.Text = client.clientPhoneLandLine;
                    txtMobile.Text = client.clientPhoneMobile;
                    txtColChoice.Text = client.clientColour;
                    txtAd1.Text = client.clientAddFirstLine;
                    txtAd2.Text = client.clientAddSecondLine;
                    txtAd3.Text = client.clientAddThirdLine;
                    txtPostCode.Text = client.clientAddPostCode;
                    txtPostCode.Text = client.clientAddPostCode;
                    txtEmailAddress.Text = client.clientAddEmail;
                }
                
            }
            else
            {
                //if the record doesn't exist (or too many are returned) jump back
                Response.Redirect("Community.aspx");
            }
            
        }

        //No parameters? Default it is then!
        
         %>

</head>
<body>
<form id="form1" runat="server">
    <table class="tableDefault" align="center" width="90%">
    <tr><td><div class="tableTitle" align="center">New Client</div></td></tr>
    <tr>
    <td>
    <div class="tableBody">
        <table width="100%">
        <tr><td colspan="4" align="center"><asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="false" />
                <tr>
    <td>Client Name:</td>
    <td><asp:TextBox ID="txtClientName" runat="server" Width="100%" MaxLength="49" Text="Name" /></td>
    <td>Organisation:</td>
    <td><asp:TextBox ID="txtClientOrg" runat="server" Width="100%" MaxLength="29" Text="Organisation" /></td>
    </tr>
    <tr>
    <td>Landline Phone:</td>
    <td><asp:TextBox ID="txtLandLine" runat="server" Width="100%" MaxLength="15" TextMode="Phone" Text="00000000000" /></td>
    <td>Mobile Phone:</td>
    <td><asp:TextBox ID="txtMobile" runat="server" Width="100%" MaxLength="15" TextMode="Phone" Text="00000000000" /></td>
    </tr>
    <tr>
    <td>Job Title:</td>
    <td><asp:TextBox ID="txtJTitle" runat="server" Width="100%" MaxLength="29" Text="Job Title" /></td>
    <td>Department:</td>
    <td><asp:TextBox ID="txtDept" runat="server" Width="100%" MaxLength="49" Text="Dept"/></td>
    </tr>
    <tr>
    <td>Address Line 1:</td>
    <td colspan="3"><asp:TextBox ID="txtAd1" runat="server" Width="100%" MaxLength="29" Text="# Street" /></td>
    </tr>
    <tr>
    <td>Address Line 2:</td>
    <td colspan="3"><asp:TextBox ID="txtAd2" runat="server" Width="100%" MaxLength="29" Text="Town" /></td>
    </tr>
    <tr>
    <td>Address Line 3:</td>
    <td colspan="3"><asp:TextBox ID="txtAd3" runat="server" Width="100%" MaxLength="29" Text="City" /></td>
    </tr>
    <tr>
    <td>Post Code:</td>
    <td><asp:TextBox ID="txtPostCode" runat="server" Width="100%" MaxLength="10" Text="Postcode" /></td>
    <td>Colour:</td>
    <td><asp:TextBox ID="txtColChoice" runat="server" Width="90%" MaxLength="7" Text="#FFFFFF" /></td>
    </tr>
    <tr>
    <td>Email:</td>
    <td colspan="3"><asp:TextBox ID="txtEmailAddress" runat="server" Width="100%" TextMode="Email" Text="email@sample.com" /></td>
    </tr>

        <tr>
        <td colspan="4">
        <table width="100%">
        <td align="center"><asp:Button ID="btnClient" Text="Submit" 
                runat="server" Width="100%" onclick="btnClient_Click" /></td>
        <td align="center"><asp:Button ID="btnCCancel" Text="Cancel" runat="server" width="100%" 
                onclick="btnCCancel_Click"/></td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
        </form>
    </div>
    </td>
    </tr>
    <tr>
    <td colspan="2"><div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div></td>
    </tr>
    </table>
</body>
</html>
